package com.xintoucloud.pbs.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xintoucloud.pbs.entity.DynamicNewsTopic;
import com.xintoucloud.pbs.service.NewsTopicManagementService;

@Controller
@RequestMapping("/NewsTopicManagement")
public class NewsTopicManagementController {
	
	@Autowired
	private NewsTopicManagementService ntmService;
	
	@RequestMapping("/index")   //连接先进到控制器, 返回值在进入要显示数据的html页面NewsTopicManagement2
	public String index() {
		return "NewsTopicManagement2";
	}
	
	@RequestMapping("/list")  //NewsTopicManagement2页面读取其中的数据,当读到URL:list时, 进入到该方法,
	@ResponseBody			//把数据封装到map中,返回的map会被自动解析	
	public Map<String,Object> list() {
		Map<String,Object> map = new HashMap<String,Object>();
		List<DynamicNewsTopic> list = ntmService.list();
		map.put("code", 0);   //1,code:解析接口状态
		map.put("data",list); //2,msg:解析提示文本
		return map;			  //3,count:解析数据长度
							  //4,data:解析数据列表
	}

	
	
	@RequestMapping("/delete")
	@ResponseBody	
	public  Map<String ,Object> delete(int ntid) {
		Map<String,Object> map = new HashMap<String,Object>();
		boolean result = ntmService.delete(ntid);
		if(result) {
			map.put("msg", "恭喜您,删除成功!");
		}else {
			map.put("msg", "很遗憾,删除失败!");
		}
		return map;
	}

	
//	@RequestMapping("/add")
//	@ResponseBody
//	public Map<String ,Object> TopAdd(DynamicNewsTopic d) {
//		Map<String,Object> map = new HashMap<String,Object>();
//		boolean result = ntmService.add(d);
//		if(result) {
//			map.put("msg", "恭喜您,添加成功!");
//		}else {
//			map.put("msg", "很遗憾,添加失败!");
//		}
//		return map;
//	}

	@RequestMapping("/add")
	public String topAdd(DynamicNewsTopic d) {
		Map<String,Object> map = new HashMap<String,Object>();
		boolean result = ntmService.add(d);
		if(result) {
			map.put("msg", "恭喜您,添加成功!");
		}else {
			map.put("msg", "很遗憾,添加失败!");
		}
		return "NewsTopicManagement2";
	}
	
	@RequestMapping("/update")
	public String topicUpdate(DynamicNewsTopic d) {
		 ntmService.update(d);
		 return "NewsTopicManagement2";
	}
}
